<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/18
 * Time: 11:42
 */

namespace Group\Controller;
use Think\Controller;
use Common\Model\OptionModel;
class MemberController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        if(!session('userdata')){
            header('location:'.U('Group/Login/index'));
            exit;
        }
    }

    public function index(){
        $id=I('get.id');
        //开始时间
        $start=I('get.start_time');
        $start=strtotime($start);
        if(empty($start)){
            $start=strtotime(date('Y-m'));
        }
        $this->assign('start',$start);

        //结束时间
        $end=I('get.end_time');
        $end=strtotime($end);
        if(empty($end)){
            $end=time();
        }
        $this->assign('end',$end);
        $mobile=I('get.mobile');

        $user = session('userdata');
        $memberarr = unserialize($user['g_member']);
        $this->assign('user',$user);
        if(empty($mobile)){
            if(in_array($id,$memberarr)){
                $level['_string'] = "(route like '{$id}') or (route regexp ',{$id},') or (route regexp '^{$id},') or (route regexp ',{$id}$')";
            }
        }else{
            $ser['mobile']=$mobile;
            $ser['nickname']=$mobile;
            $ser['_logic']='or';
            $searchdata=M('follow')->field('id,route')->where($ser)->find();
            $roudata=explode(',',$searchdata['route']);
            foreach($roudata as $val){
                if(in_array($val,$memberarr)){
                    $pan=true;
                    break;
                }
            }
            if($pan){
                $level['id']=$searchdata['id'];
            }else{
                $this->error('对不起,没有该成员');
            }

        }
        //获取组成员下的用户
            $level['mobile']=array('neq',"");
            $followdata=M('follow')->field('id')->where($level)->select();
            foreach($followdata as $x){
                $inarr[]=$x['id'];
            }
            $count=M('follow')->where($level)->count();
            $page=new \Think\Page($count,10);
            $follow=M('follow')->field('id,nickname,mobile,money,subscribe_time')->where($level)->limit($page->firstRow.','.$page->listRows)->select();
            foreach($follow as $value){

                $member['id']=$value['id'];
                $member['nickname']=$value['nickname'];
                $member['mobile']=$value['mobile'];
                $member['money']=$value['money'];
                $member['subscribe_time']=$value['subscribe_time'];
                $showdata[]=$member;
            }

        $this->assign('page',$page);
        $this->assign('showdata',$showdata);

        //获取所有用户的充值记录
        $rechg['user_id']=array('in',$inarr);
        $rechg['is_pay']=1;
        $rechg['create_time']=array(array('egt',$start),array('elt',$end+86400));
        $rechargedata=M('futures_recharge')->field('sum(money) as money')->where($rechg)->find();


        //获取所有用户的提现记录
        $drawals['user_id']=array('in',$inarr);
        $drawals['is_pay']=1;
        $drawals['pay_time']=array(array('egt',$start),array('elt',$end+86400));
        $drawalsdata=M('futures_withdrawals')->field('sum(money) as money')->where($drawals)->find();


        //获取所有用户的佣金
        $coms['commission_user_id']=array('in',$inarr);
        $coms['create_time']=array(array('egt',$start),array('elt',$end+86400));
        $commissiondata=M('futures_commission')->field('sum(commission) as money')->where($coms)->group('status')->select();

        //获取恒指订单信息
        $henmodel=new OptionModel('order');
        if(!empty($inarr)) {


            $wherehen['user_id'] = array('in', $inarr);
            $wherehen['start_time'] = array('$gte' => $start, '$lte' => $end + 86400);
            $hendata = $henmodel->where($wherehen)->field('invest,profit')->select();
            $henorderprice = 0;
            foreach ($hendata as $v) {
                $henorderprice += $v['invest'];
            }
            $showhendata = array($henorderprice);


            //获取欧元订单信息
            $euromodel = new OptionModel('euro_order');
            $eurodata = $euromodel->field('invest,profit')->where($wherehen)->select();
            $euroorderprice = 0;

            foreach ($eurodata as $v) {
                $euroorderprice += $v['invest'];

            }
            $showeurodata = array($euroorderprice);

            //获取伦敦金订单信息
            $goldmodel = new OptionModel('goldorder');
            $golddata = $goldmodel->field('invest,profit')->where($wherehen)->select();
            $goldorderprice = 0;

            foreach ($golddata as $v) {
                $goldorderprice += $v['invest'];
            }
            $showgolddata = array($goldorderprice);

            //获取美元订单信息
            $dollarmodel = new OptionModel('dollarorder');
            $dollardata = $dollarmodel->field('invest,profit')->where($wherehen)->select();
            $dollarorderprice = 0;

            foreach ($dollardata as $v) {
                $dollarorderprice += $v['invest'];
            }
            $showdollardata = array($dollarorderprice);

            $this->assign('showdollardata',$showdollardata);
            $this->assign('showhendata',$showhendata);
            $this->assign('showeurodata',$showeurodata);
            $this->assign('showgolddata',$showgolddata);
        }

        $this->assign('rechargedata',$rechargedata);
        $this->assign('drawalsdata',$drawalsdata);
        $this->assign('commissiondata',$commissiondata);

        $this->display();
    }

    public function member_only(){
        $id=I('get.id');
        $shangji=I('get.shangji');
        //开始时间
        $start=I('get.start_time');
        $start=strtotime($start);
        if(empty($start)){
            $start=strtotime(date('Y-m'));
        }
        $this->assign('start',$start);

        //结束时间
        $end=I('get.end_time');
        $end=strtotime($end);
        if(empty($end)){
            $end=time();
        }
        $this->assign('end',$end);
        $mobile=I('get.mobile');

        $user = session('userdata');
        $memberarr = unserialize($user['g_member']);
        $this->assign('user',$user);

        if(empty($mobile)){
            $level['id']=$id;
        }else{
            $ser['mobile']=$mobile;
            $ser['nickname']=$mobile;
            $ser['_logic']='or';
            $searchdata=M('follow')->field('id,route')->where($ser)->find();
            $roudata=explode(',',$searchdata['route']);
            foreach($roudata as $val){
                if(in_array($val,$memberarr)){
                    $pan=true;
                    break;
                }
            }
            if($pan){
                $level['id']=$searchdata['id'];
            }else{
                $this->error('对不起,没有该成员');
            }

        }
        //获取组成员下的用户

        $follow=M('follow')->field('id,nickname,mobile,money,subscribe_time,route')->where($level)->select();

        foreach($follow as $value){
            if(empty($shangji)){
                $pop=explode(',',$value['route']);
                $pid=array_pop($pop);
                $shangjidata=M('follow')->find($pid);
                $shangji=$shangjidata['nickname'];
            }
            $member['id']=$value['id'];
            $member['nickname']=$value['nickname'];
            $member['mobile']=$value['mobile'];
            $member['money']=$value['money'];
            $member['subscribe_time']=$value['subscribe_time'];
            $member['shangji']=$shangji;
            $showdata[]=$member;
        }
        $this->assign('showdata',$showdata);
        //获取所有用户的充值记录
        $rechg['user_id']=$id;
        $rechg['is_pay']=1;
        $rechg['create_time']=array(array('egt',$start),array('elt',$end+86400));
        $rechargedata=M('futures_recharge')->field('sum(money) as money')->where($rechg)->find();


        //获取所有用户的提现记录
        $drawals['user_id']=$id;
        $drawals['is_pay']=1;
        $drawals['pay_time']=array(array('egt',$start),array('elt',$end+86400));
        $drawalsdata=M('futures_withdrawals')->field('sum(money) as money')->where($drawals)->find();


        //获取所有用户的佣金
        $coms['commission_user_id']=$id;
        $coms['create_time']=array(array('egt',$start),array('elt',$end+86400));
        $commissiondata=M('futures_commission')->field('sum(commission) as money')->where($coms)->group('status')->select();

        //获取恒指订单信息
        $henmodel=new OptionModel('order');
        $wherehen['user_id']=$id;
        $wherehen['start_time']=array('$gte'=>$start,'$lte'=>$end+86400);
        $hendata=$henmodel->where($wherehen)->field('invest,profit')->select();
        $henorderprice=0;

        foreach($hendata as $v){
            $henorderprice+=$v['invest'];

        }
        $showhendata=array($henorderprice);


        //获取欧元订单信息
        $euromodel=new OptionModel('euro_order');
        $eurodata=$euromodel->field('invest,profit')->where($wherehen)->select();
        $euroorderprice=0;

        foreach($eurodata as $v){
            $euroorderprice+=$v['invest'];
        }
        $showeurodata=array($euroorderprice);

        //获取伦敦金订单信息
        $goldmodel=new OptionModel('goldorder');
        $golddata=$goldmodel->field('invest,profit')->where($wherehen)->select();
        $goldorderprice=0;

        foreach($golddata as $v){
            $goldorderprice+=$v['invest'];
        }
        $showgolddata=array($goldorderprice);

        //获取美元订单信息
        $dollarmodel=new OptionModel('dollarorder');
        $dollardata=$dollarmodel->field('invest,profit')->where($wherehen)->select();
        $dollarorderprice=0;

        foreach($dollardata as $v){
            $dollarorderprice+=$v['invest'];
        }
        $showdollardata=array($dollarorderprice);

        $this->assign('rechargedata',$rechargedata);
        $this->assign('drawalsdata',$drawalsdata);
        $this->assign('commissiondata',$commissiondata);
        $this->assign('showhendata',$showhendata);
        $this->assign('showeurodata',$showeurodata);
        $this->assign('showgolddata',$showgolddata);
        $this->assign('showdollardata',$showdollardata);
        $this->display();
    }


}